﻿/*
Copyright © 2011 François Karman

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

See <license.txt> for the complete license of the software.
*/

using System;

namespace Simple.Storage
{
    /// <summary>
    /// Provides data for the <see cref="IDataSource.Loaded"/>,
    /// <see cref="IDataSource.Saving"/>, <see cref="IDataSource.Saved"/>
    /// events.
    /// </summary>
    /// <seealso cref="IDataSource"/>
    public class InstanceEventArgs : EventArgs
    {
        /// <summary>
        /// The type of the associated persisted class.
        /// </summary>
        private readonly Type classType;

        /// <summary>
        /// The persisted instance.
        /// </summary>
        private readonly object instance;

        /// <summary>
        /// Initializes a new instance of the InstanceEventArgs class.
        /// </summary>
        /// <param name="classType">The type of the associated persisted class.</param>
        /// <param name="instance">The persisted instance.</param>
        /// <exception cref="ArgumentNullException">
        /// One of the parameter is <c>null</c>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// The <paramref name="instance"/> is not an instance of <paramref name="classType"/>.
        /// </exception>
        public InstanceEventArgs(Type classType, object instance)
        {
            if (classType == null)
            {
                throw new ArgumentNullException("classType");
            }
            else if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            else if (!classType.IsInstanceOfType(instance))
            {
                string message = string.Format(
                    Messages.Culture,
                    Messages.InstanceEventArgsArgument,
                    classType.FullName,
                    instance.GetType().FullName);
                throw new ArgumentException(message, "instance");
            }

            this.classType = classType;
            this.instance = instance;
        }

        /// <summary>
        /// Gets the type of the associated persisted class.
        /// </summary>
        public Type ClassType
        {
            get { return this.classType; }
        }

        /// <summary>
        /// Gets the persisted instance.
        /// </summary>
        public object Instance
        {
            get { return this.instance; }
        }
    }
}
